﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.admin.coupon
{
    public partial class sendcoupon : MxWeiXinPF.Web.UI.ManagePage
    {
        private int userid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.userid = MXRequest.GetQueryInt("userid");
            if (this.userid == 0)
            {
                JscriptMsg("传输参数不正确！", "back", "Error");
                return;
            }
            if (!new BLL.users().Exists(this.userid))
            {
                JscriptMsg("用户不存在！", "back", "Error");
                return;
            }
            if (!Page.IsPostBack)
            {
                TreeBind();
            }
        }

        #region 绑定类别=================================
        private void TreeBind()
        {
            BLL.wx_shop_coupon bll = new BLL.wx_shop_coupon();

            Model.manager weixin = base.GetAdminInfo();
            DataSet ds = bll.GetList(" status = 1 and timeend >'" + DateTime.Now.ToString() + "' ");
            this.ddlCouponId.Items.Clear();
            this.ddlCouponId.Items.Add(new ListItem("未选择", "0"));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["couponname"].ToString().Trim();
                this.ddlCouponId.Items.Add(new ListItem(Title, Id));
            }

        }
        #endregion

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.ddlCouponId.SelectedValue == "0")
            {
                base.JscriptMsg("送券失败,用户领券数量大于设定值！", Utils.CombUrlTxt("sendcoupon.aspx", "userid={0}", new string[] { this.userid.ToString() }), "Success");
                return;
            }
            SendTicket();
        }

        private void SendTicket()
        {
            BLL.users buser = new BLL.users();
            string couponid = this.ddlCouponId.SelectedValue;
            BLL.wx_shop_coupon bcoupon = new BLL.wx_shop_coupon();
            Model.wx_shop_coupon mcoupon = bcoupon.GetModel(int.Parse(couponid));
            Model.users muser = buser.GetModel(Convert.ToInt32(userid));
            BLL.wx_shop_coupon_data bcoupondata = new BLL.wx_shop_coupon_data();
            if (muser != null && mcoupon != null)
            {
                DataSet ds = bcoupondata.GetList(string.Format("senduid = {0} and couponid={1} ", userid, mcoupon.id));
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    List<Model.wx_shop_coupon_data> datas = bcoupondata.DataTableToList(ds.Tables[0]);
                    if (datas.Count >= mcoupon.numperuser)
                    {
                        base.JscriptMsg("送券失败,用户领券数量大于设定值！", Utils.CombUrlTxt("sendcoupon.aspx", "userid={0}", new string[] { this.userid.ToString() }), "Success");
                        return;
                    }
                    else
                    {
                        Model.wx_shop_coupon_data thecoupon = new Model.wx_shop_coupon_data();
                        thecoupon.couponid = mcoupon.id;
                        thecoupon.shopid = base.GetAdminInfo().id;
                        thecoupon.openid = muser.wxOpenId;
                        thecoupon.gettype = 0;
                        thecoupon.status = 0;
                        thecoupon.gettime = DateTime.Now;
                        thecoupon.usetime = DateTime.Now;
                        thecoupon.senduid = muser.id;
                        thecoupon.ordersn = 0;
                        thecoupon.sendmsgstate = 0;
                        thecoupon.sendmsgtime = DateTime.Now;

                        if (bcoupondata.Add(thecoupon) > 0)
                        {

                            //送券成功,增加日志记录
                            Model.wx_shop_coupon_log log = new Model.wx_shop_coupon_log();
                            log.shopid = base.GetAdminInfo().id;
                            log.openid = muser.wxOpenId;
                            log.times = DateTime.Now;
                            log.couponid = mcoupon.id;
                            log.orderid = 0;
                            log.explain = "后台赠送用户";
                            log.remark = " ";
                            log.exstr1 = " ";
                            new BLL.wx_shop_coupon_log().Add(log);
                            base.JscriptMsg("送券成功", Utils.CombUrlTxt("../users/user_list.aspx", "userid={0}", new string[] { this.userid.ToString() }), "Success");
                            return;
                        }

                    }
                }
            }
            base.JscriptMsg("送券失败,用户领券数量大于设定值！", Utils.CombUrlTxt("sendcoupon.aspx", "userid={0}", new string[] { this.userid.ToString() }), "Success");
            return;
        }
    }
}